﻿using System;
using System.Web;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using DevExpress.Web.ASPxCallback;
using System.Collections.Specialized;
using SGRapidForm.Permission.Entity;

namespace SGDemo.Web
{
    public partial class Login : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.UserHostAddress.Equals("110.80.17.26"))
            {
                this.Response.Redirect("http://192.168.69.239:2234/", true);
            }
        }

        /// <summary>
        /// 登录事件
        /// </summary>
        protected void cabLogin_Callback(object sender, CallbackEventArgs e)
        {
            NameValueCollection parameters = HttpUtility.ParseQueryString(e.Parameter);
            //验证验证码是否正确
            if (parameters["code"] != capCode.Code)
            {
                e.Result = "{\"isSuccess\":false, \"msg\":\"提示：验证码输入不正确\"}";
                return;
            }
            //验证用户名是否存在
            bool isExist = _permissionService.User_IsExist(parameters["username"]);
            if (!isExist)
            {
                e.Result = "{\"isSuccess\":false, \"msg\":\"提示：该用户名不存在!\"}";
                return;
            }
            UserDetails user = _permissionService.User_GetByName(parameters["username"]);
            //验证密码是否正确
            if (user.Password != DESEncrypt.Encrypt(parameters["password"]))
            {
                e.Result = "{\"isSuccess\":false, \"msg\":\"提示：用户名或者密码不正确!\"}";
                return;
            }
            //验证用户是否通过审核
            if (!user.IsApproved)
            {
                e.Result = "{\"isSuccess\":false, \"msg\":\"提示：您的帐号还未通过审核，请联系管理员\"}";
                return;
            }
            //验证用户是否被锁定
            if (user.IsLockedOut)
            {
                e.Result = "{\"isSuccess\":false, \"msg\":\"提示：您的帐号已被系统锁定，请联系管理员\"}";
                return;
            }

            //登录成功后，跳转地址
            string url = string.IsNullOrEmpty(user.PasswordQuestion) ? "FillQuestion.aspx" : "ChooseToDept.aspx";

            //登录成功
            e.Result = "{\"isSuccess\":true, \"msg\":\"提示：登录成功!正在跳转...\", \"url\":\"" + url + "\"}";
            RecordInfoToSession(user);
            UpdateSessionID(user);
        }

        /// <summary>
        /// 初始化Session信息
        /// </summary>
        /// <param name="user"></param>
        private void RecordInfoToSession(UserDetails user)
        {
            ApplicationContext.Current.SessionID = this.Session.SessionID;
            ApplicationContext.Current.UserKeyid = user.Keyid;
            ApplicationContext.Current.UserName = user.Username;
            ApplicationContext.Current.RealName = user.Realname;
            ApplicationContext.Current.DeptKeyid = user.DeptKeyid;
            ApplicationContext.Current.DeptCode = user.DeptCode;
            ApplicationContext.Current.DeptName = user.DeptName;
            ApplicationContext.Current.Email = user.Email;
            ApplicationContext.Current.MobilePhone = user.MobilePhone;
            ApplicationContext.Current.PasswordAnswer = user.PasswordAnswer;
            ApplicationContext.Current.PasswordQuestion = user.PasswordQuestion;
        }

        /// <summary>
        /// 更新SessionID
        /// </summary>
        /// <param name="user"></param>
        private void UpdateSessionID(UserDetails user)
        {
            user.SessionID = this.Session.SessionID;
            _permissionService.User_Update(user);
        }
    }
}